]> git.r.bdr.sh - rbdr/map/blame - Map/Presentation/Base Components/MapRender/MapEdges.swift
Address the lint warnings
[rbdr/map] / Map / Presentation / Base Components / MapRender / MapEdges.swift
CommitLineData
be897af3
RBR
1// Copyright (C) 2024 Rubén Beltrán del Río
2
3// This program is free software: you can redistribute it and/or modify
4// it under the terms of the GNU General Public License as published by
5// the Free Software Foundation, either version 3 of the License, or
6// (at your option) any later version.
7
8// This program is distributed in the hope that it will be useful,
9// but WITHOUT ANY WARRANTY; without even the implied warranty of
10// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11// GNU General Public License for more details.
12
13// You should have received a copy of the GNU General Public License
14// along with this program. If not, see https://map.tranquil.systems.
5e8ff485
RBR
15import SwiftUI
16
17struct MapEdges: View {
18
5e8ff485
RBR
19 let mapSize: CGSize
20 let lineWidth: CGFloat
21 let vertexSize: CGSize
22 let edges: [MapEdge]
23
24 let arrowheadSize = CGFloat(10.0)
25
5e8ff485
RBR
26 var body: some View {
27 ForEach(edges, id: \.id) { edge in
28 Path { path in
29
30 // First we transform edges from percentage to map coordinates
31 let origin = CGPoint(x: w(edge.origin.x), y: h(edge.origin.y))
32 let destination = CGPoint(x: w(edge.destination.x), y: h(edge.destination.y))
33
34 let slope = (destination.y - origin.y) / (destination.x - origin.x)
35 let angle = atan(slope)
36 let multiplier = CGFloat(slope < 0 ? -1.0 : 1.0)
37 let upperAngle = angle - CGFloat.pi / 4.0
38 let lowerAngle = angle + CGFloat.pi / 4.0
39
40 let offsetOrigin = CGPoint(
41 x: origin.x + multiplier * (vertexSize.width / 2.0) * cos(angle),
42 y: origin.y + multiplier * (vertexSize.height / 2.0) * sin(angle))
43 let offsetDestination = CGPoint(
44 x: destination.x - multiplier * (vertexSize.width / 2.0) * cos(angle),
45 y: destination.y - multiplier * (vertexSize.height / 2.0) * sin(angle))
46
47 path.move(to: offsetOrigin)
48 path.addLine(to: offsetDestination)
49
50 if edge.arrowhead {
51 path.move(to: offsetDestination)
52 path.addLine(
53 to: CGPoint(
54 x: offsetDestination.x - multiplier * arrowheadSize * cos(upperAngle),
55 y:
56 offsetDestination.y - multiplier * arrowheadSize * sin(upperAngle)))
57
58 path.move(to: offsetDestination)
59 path.addLine(
60 to: CGPoint(
61 x: offsetDestination.x - multiplier * arrowheadSize * cos(lowerAngle),
62 y:
63 offsetDestination.y - multiplier * arrowheadSize * sin(lowerAngle)))
64 }
65 path.move(to: offsetDestination)
66 path.closeSubpath()
67 }.applying(
68 CGAffineTransform(translationX: vertexSize.width / 2.0, y: vertexSize.height / 2.0)
be897af3 69 ).stroke(Color.Map.vertexColor, lineWidth: lineWidth)
5e8ff485
RBR
70 }
71 }
72
73 func h(_ dimension: CGFloat) -> CGFloat {
74 max(0.0, min(mapSize.height, dimension * mapSize.height / 100.0))
75 }
76
77 func w(_ dimension: CGFloat) -> CGFloat {
78 max(0.0, min(mapSize.width, dimension * mapSize.width / 100.0))
79 }
80}
81
e2c37ac1
RBR
82#Preview {
83 MapEdges(
84 mapSize: CGSize(width: 400.0, height: 400.0), lineWidth: 1.0,
85 vertexSize: CGSize(width: 25.0, height: 25.0),
86 edges: [
87 MapEdge(
88 id: 1, origin: CGPoint(x: 2.0, y: 34.0), destination: CGPoint(x: 23.0, y: 76.2),
89 arrowhead: true)
90 ])
5e8ff485 91}